package com.woniuxy.controller;

import com.woniuxy.entity.Perm;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacPermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName RbacPermController
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/9/3 15:18
 * @Version 1.0
 */
@RequestMapping("/rbacPerm")
@RestController
@CrossOrigin
public class RbacPermController {
    @Autowired
    RbacPermService permService;

    @PostMapping("/addOne")
    public ResponseEntity addOne(@RequestBody Perm perm){
        System.out.println(perm.toString());
        List<Perm> list= findUserByName(perm.getName());
        if(list.size()!=0){
            return new ResponseEntity(500,"用户名重复",null);
        }else {
            return permService.addPerm(perm);
        }
    }
    @GetMapping("/selectAll/{currentPage}/{pageSize}")
    public ResponseEntity findAll(@PathVariable("currentPage")int currentPage,@PathVariable("pageSize") int pageSize) {
        return permService.selectAll(currentPage,pageSize);
    }
    @PostMapping("/editOne")
    public ResponseEntity editOne(@RequestBody Perm perm){
        return permService.editOne(perm);
    }
    @GetMapping("/search/{currentPage}/{pageSize}")
    public ResponseEntity search(@PathVariable("currentPage")int currentPage,@PathVariable("pageSize") int pageSize,@RequestParam String name,@RequestParam String code){
        return permService.search(currentPage,pageSize,name,code);
    }
    @PostMapping("/permDelete")
    public ResponseEntity permDelete(String id){
        return permService.deletePermById(Integer.parseInt(id));
    }
    public List<Perm> findUserByName(String username){
        List<Perm> list = permService.findPermByName(username);
        return list;
    }
}
